package com.yhos.platform.oopston.core.aop;


import java.util.Map;

public class ParamsThreadLocalManager {

    private static ParamsThreadLocalManager manager = null;

    private InheritableThreadLocal<ParamsBean> threadLocal = new InheritableThreadLocal<>();

    private ParamsThreadLocalManager() {
    }

    public static synchronized ParamsThreadLocalManager getInstance() {
        if (manager == null) {
            manager = new ParamsThreadLocalManager();
        }
        return manager;
    }

    public void putRequestParams(Map<String, Object> requestParamsMap) {
        putParams(requestParamsMap, null);
    }

    public void putResponseParams(Map<String, Object> responseParamsMap) {
        ParamsBean params = threadLocal.get();
        if (params == null) {
            params = new ParamsBean();
        }
        params.setResponseParamsMap(responseParamsMap);
        threadLocal.set(params);
    }

    public void putParams(Map<String, Object> requestParamsMap, Map<String, Object> extDefineParamsMap) {
        ParamsBean params = new ParamsBean();
        if (requestParamsMap != null) {
            params.setRequestParamsMap(requestParamsMap);
        }
        if (extDefineParamsMap != null) {
            params.setExtDefineParamsMap(extDefineParamsMap);
        }
        threadLocal.set(params);
    }

    public ParamsBean getParams() {
        return threadLocal.get();
    }

    public void removeParams() {
        threadLocal.set(null);
    }

}
